<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}`">
    <el-space direction="vertical">
      <el-descriptions :column="1" title="调拨单信息" border>
        <el-descriptions-item width="300" label="订单序号">{{ drawerProps.row.orderId }}</el-descriptions-item>
        <el-descriptions-item width="300" label="任务序号">{{ drawerProps.row.taskId }}</el-descriptions-item>
        <el-descriptions-item width="300" label="调拨单状态">
          <el-tag v-if="drawerProps.row?.transferStatus === 0" type="warning">待调度</el-tag>
          <el-tag v-if="drawerProps.row?.transferStatus === 1" type="success">已调度</el-tag>
          <el-tag v-if="drawerProps.row?.transferStatus === 2" type="danger">挂起</el-tag>
        </el-descriptions-item>
        <el-descriptions-item width="300" label="创建时间">{{ drawerProps.row?.createTime }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions :column="1" title="订单项信息" border>
        <el-descriptions-item width="300" label="商品名称">{{ drawerProps.row.orderItems?.[0]?.goodsName }}</el-descriptions-item>
        <el-descriptions-item width="300" label="商品价格">{{
          drawerProps.row.orderItems?.[0]?.goodsPrice
        }}</el-descriptions-item>
        <el-descriptions-item width="300" label="商品数量">{{ drawerProps.row.orderItems?.[0]?.count }}</el-descriptions-item>
        <el-descriptions-item width="300" label="计量单位">{{
          drawerProps.row.orderItems?.[0]?.measureUnit
        }}</el-descriptions-item>
      </el-descriptions>
      <p></p>
      <el-form
        ref="ruleFormRef"
        label-width="150px"
        label-suffix=" :"
        :model="drawerProps.row"
        :hide-required-asterisk="drawerProps.isView"
        ><el-form-item label="出库库号" prop="drawerProps.row.outboundWarehouseId" style="display: flex">
          <el-input
            v-model="drawerProps.row.outboundWarehouseId"
            placeholder="请选择出库库房"
            clearable
            style="flex: 1"
          ></el-input>
          <el-button type="primary" @click="openWarehouseDialog(0)">选择出库库房</el-button>
        </el-form-item>
        <el-form-item label="入库库号" prop="drawerProps.row.inboundWarehouseId" style="display: flex; align-items: center">
          <el-input
            v-model="drawerProps.row.inboundWarehouseId"
            placeholder="请选择入库库房"
            clearable
            style="flex: 1"
          ></el-input>
          <el-button type="primary" @click="openWarehouseDialog(1)">选择入库库房</el-button>
        </el-form-item>
      </el-form>
    </el-space>
    <template #footer>
      <el-button type="primary" @click="handleSubmit">确定调拨</el-button>
      <el-button @click="drawerVisible = false">取消</el-button>
    </template>
    <WarehouseDialog ref="warehouseRef" @get-warehouse="getWarehouse" />
  </el-drawer>
</template>

<script setup lang="ts" name="EditDrawer">
import { ref } from "vue";
import { TransferOrder, Warehouse } from "@/api/interface";
import WarehouseDialog from "./WarehouseDialog.vue";
import { ElMessage, FormInstance } from "element-plus";

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<TransferOrder.ResTransferOrderList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const drawerProps2 = ref<TransferOrder.ReqTransferOrderEntity>({
        transferOrderId: drawerProps.value.row.transferOrderId ?? 0,
        transferType: drawerProps.value.row.transferType ?? 0,
        orderId: drawerProps.value.row.orderId ?? 0,
        outboundWarehouseId: drawerProps.value.row.outboundWarehouseId ?? 0,
        inboundWarehouseId: drawerProps.value.row.inboundWarehouseId ?? 0,
        goodsItems: [
          {
            goodsId: drawerProps.value.row.orderItems?.[0]?.goodsId,
            goodsName: drawerProps.value.row.orderItems?.[0]?.goodsName,
            count: drawerProps.value.row.orderItems?.[0]?.count,
            measureUnit: drawerProps.value.row.orderItems?.[0]?.measureUnit
          }
        ]
      });
      await drawerProps.value.api!(drawerProps2.value);
      ElMessage.success({ message: `调拨成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});

const warehouseRef = ref<InstanceType<typeof WarehouseDialog> | null>(null);
const openWarehouseDialog = (value: number) => {
  console.log(1324234234234231);
  warehouseRef.value?.open(value);
};

const getWarehouse = (row: Warehouse.ResWarehouseList) => {
  if (drawerProps.value.row.outboundWarehouseId) {
    drawerProps.value.row.outboundWarehouseId = row.warehouseId;
    //drawerProps.value.row.outboundWarehouseId = row.warehouseName;
  }
};
</script>
